package com.redteam.data.dao;

import com.redteam.data.beans.MaintenanceEngineer;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * DAO for retrieving <code>MaintenanceEngineer</code> (mechanics) data
 *
 * @author Jezreel Wilson
 * @author Steven Lawton
 * @version 0.9
 */
public class MechanicDAO extends AbstractDAO implements DAO {

    private static MechanicDAO dao;
    private static DataSource ds;
    private final String tableName;

    static {
        dao = new MechanicDAO();
    }

    /**
     * Returns the instance of this DAO.
     *
     * @return Singleton instance for use
     */
    public static MechanicDAO getInstance() {
        return dao;
    }

    private MechanicDAO() {
        super();
        getDataSource();
        this.tableName = "HIGHFLIERS.Persons";
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/redteam");

        } catch (NamingException ex) {
            Logger.getLogger(MechanicDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public MaintenanceEngineer getObjectOut(ResultSet rs) throws SQLException {
 
        MaintenanceEngineer me = new MaintenanceEngineer();
        me.setId(rs.getLong("ID"));
        me.setFirstName(rs.getString("FIRSTNAME"));
        me.setMiddleName(rs.getString("MIDDLENAME"));
        me.setLastName(rs.getString("LASTNAME"));
        me.setStreet(rs.getString("STREET"));
        me.setCity(rs.getString("CITY"));
        me.setState(rs.getString("STATE"));
        me.setZipCode(rs.getString("ZIPCODE"));
        me.setEmail(rs.getString("EMAIL"));
        me.setfBOId(rs.getLong("FBO_ID"));
        me.setBirthDate(rs.getDate("BIRTHDATE"));
        me.setHomePhone(rs.getString("HOMEPHONE"));
        me.setCellPhone(rs.getString("CELLPHONE"));
        me.setWorkPhone(rs.getString("WORKPHONE"));
        me.setGender(rs.getString("GENDER"));
        me.setDiscriminator(rs.getString("DISCRIMINATOR"));
        me.setUsername(rs.getString("USER_NAME"));
        me.setPassword(rs.getString("PASSWORD"));

        return me;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildInsertQuery(Object objectToInsert) {
        MaintenanceEngineer person = (MaintenanceEngineer) objectToInsert;
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO ").append(this.getTableName());
        sb.append(" (FIRSTNAME,MIDDLENAME,LASTNAME,STREET,CITY,STATE,ZIPCODE,EMAIL,FBO_ID,");
        sb.append("BIRTHDATE,HOMEPHONE,CELLPHONE,WORKPHONE,DISCRIMINATOR,GENDER,USERNAME,PASSWORD)");
        sb.append(" VALUES (");
        sb.append(person.getFirstName()).append("',' ");
        sb.append(person.getMiddleName()).append("','");
        sb.append(person.getLastName()).append("','");
        sb.append(person.getStreet()).append("','");
        sb.append(person.getCity()).append("','");
        sb.append(person.getState()).append("','");
        sb.append(person.getZipCode()).append("','");
        sb.append(person.getEmail()).append("',");
        sb.append(person.getfBOId()).append(",'");
        sb.append(person.getBirthDate()).append("','");
        sb.append(person.getHomePhone()).append("','");
        sb.append(person.getCellPhone()).append("','");
        sb.append(person.getWorkPhone()).append("','");
        sb.append(person.getDiscriminator()).append("','");
        sb.append(person.getGender()).append("','");
        sb.append(person.getUsername()).append("','");
        sb.append(person.getPassword()).append("')");

        return new String(sb);
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildUpdateQuery(Object objectToUpdate) {
        MaintenanceEngineer person = (MaintenanceEngineer) objectToUpdate;
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(this.getTableName()).append(" SET FIRSTNAME = '").append(person.getFirstName());
        sb.append("',MIDDLENAME='").append(person.getMiddleName());
        sb.append("',LASTNAME='").append(person.getLastName()).append("',STREET='");
        sb.append(person.getStreet()).append("',CITY='").append(person.getCity());
        sb.append("',STATE='").append(person.getState()).append("',ZIPCODE='");
        sb.append(person.getZipCode()).append("',EMAIL='").append(person.getEmail());
        sb.append("',FBO_ID=").append(person.getfBOId());
        sb.append(",BIRTHDATE='").append(person.getBirthDate());
        sb.append("',HOMEPHONE='").append(person.getHomePhone());
        sb.append("',CELLPHONE='").append(person.getCellPhone());
        sb.append("',WORKPHONE='").append(person.getWorkPhone());
        sb.append("',GENDER='").append(person.getGender());
        sb.append("',DISCRIMINATOR='").append(person.getDiscriminator());
        sb.append("',USERNAME='").append(person.getUsername());
        sb.append("',PASSWORD='").append(person.getPassword());
        sb.append("' WHERE ID= ").append(person.getId());

        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public long getObjectId(Object obj) {
        MaintenanceEngineer mechanic = (MaintenanceEngineer) obj;
        return mechanic.getId();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String getTableName() {
        return this.tableName;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public DataSource getDS() {
        return MechanicDAO.ds;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public List<Object> getAll() throws SQLException {
        return getAll("mechanic");
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public Object getSingle(long id) throws SQLException {
        return getSingle(id, "mechanic");
    }

}
